
package de.unihannover.se.restlab.microblog;

import java.util.LinkedList;
import java.util.List;

import javax.ws.rs.ApplicationPath;

import com.sun.jersey.api.core.PackagesResourceConfig;

import de.unihannover.se.restlab.microblog.data.AccountStorage;
import de.unihannover.se.restlab.microblog.data.MessageStorage;
import de.unihannover.se.restlab.microblog.data.UserStorage;
import de.unihannover.se.restlab.microblog.models.Message;
import de.unihannover.se.restlab.microblog.models.Role;
import de.unihannover.se.restlab.microblog.models.User;

/** Mit Testdaten initialisieren.
 * @author restlab_c */
@ApplicationPath("/")
public class ApplicationConfig extends PackagesResourceConfig {

	private List<User> users = new LinkedList<User>();

	public ApplicationConfig () {
		super("de.unihannover.se.restlab.microblog.resources;" + "de.unihannover.se.restlab.microblog.providers");

		//clearTables();
		//setupExampleData();
		loadFromDB();
	}

	private void loadFromDB () {
		UserStorage.getStorage().setup();
		MessageStorage.getStorage().setup();
		AccountStorage.getStorage().setup();
	}

	private void clearTables () {
		UserStorage.getStorage().clear();
		MessageStorage.getStorage().clear();
		AccountStorage.getStorage().clear();
	}

	@SuppressWarnings("unused")
	private void setupExampleData () {
		String[] userNames = {"Hans", "Peter", "Harald", "Uwe", "Bernd", "Ignatius"};

		// Hans ist Admin, Ignatius ist Gast und der Rest sind normale User.
		for (String name : userNames) {
			// TODO: In Zukunft lieber ifs verwenden
			AccountStorage.getStorage().createAccount(u(name),
				name.equals("Hans") ? Role.Admin : name.equals("Ignatius") ? Role.Guest : Role.User);
		}

		for (int i = 0; i < users.size(); i++)
			m(i);
	}

	private User u (String name) {
		User user = UserStorage.getStorage().createUser(name);
		users.add(user);
		return user;
		/* Link self = new Link(ui.getAbsolutePathBuilder().path("{id}").build(user.getId()).toString(), "self");
		 * user.setSelf(self); */
	}

	// @Context UriInfo ui;

	private void m (int userIndex) {
		@SuppressWarnings("unused")
		Message msg = MessageStorage.getStorage().createMessage(users.get(userIndex), "Nachricht von " + (userIndex + 1), -1);

/* Link self = new Link(ui.getAbsolutePathBuilder().path("{id}").build(msg.getId()).toString(), "self"); msg.setSelf(self); */
	}

}
